package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.domain.dto.GoodsMessage;
import org.example.domain.entity.*;
import org.example.domain.model.ShoppingCartAllInfo;
import org.example.mapper.*;
import org.example.service.IOrderMessageService;
import org.example.service.IShoppingCartAllInfoService;
import org.example.util.OrderNumber;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.event.ItemEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
szy

*/
@Service
public class OrderMessageServiceImpl implements IOrderMessageService {
    @Resource
    private OrderNumber orderNumber;

    @Resource
    private LoginNameMapper loginNameMapper;

    @Resource
    private ProductIdMapper productIdMapper;

    //购物车实体类的Mapper
    private static final String url="https://fzlbkt.oss-cn-hangzhou.aliyuncs.com/exampledir/";

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderItemMapper orderItemMapper;

    @Resource
    private ProductImgMapper productImgMapper;

    @Resource
    private ProductSkuMapper productSkuMapper;

    @Resource
    private IShoppingCartAllInfoService iShoppingCartAllInfoService;

    @Resource
    private UserAddrMapper userAddrMapper;

    @Resource
    private UsersMapper usersMapper;

    //生成订单项  并存入数据库中
    @Override
    public String orderList(GoodsMessage goodsMessage) {
        //生成订单号

        System.out.println("asdljasdkaf"+goodsMessage.getUserId());
        String s1 = orderNumber.orderId();
        List<OrderItem> orderItemList = new ArrayList<>();
        //获取订单信息
        Order order = orderAdd(goodsMessage, s1);
        for(int i=0;i<goodsMessage.getShopId().split(",").length;i++){
            OrderItem orderItem = orderItemAdd(goodsMessage, s1, i);
            //将对象添加到列表中
            orderItemList.add(orderItem);
        }
//        System.out.println(goodsMessage.getItemId());

        order.setStatus(1);//未支付
        for (OrderItem orderItem : orderItemList) {
            if (order.getUntitled()!=null){
                order.setUntitled(order.getUntitled()+","+orderItem.getProductName());
            }
            else {
                order.setUntitled(orderItem.getProductName());
            }
//            order.setTotalAmount(productSkuMapper.selectById(orderItem.getProductId()));
            orderItemMapper.insert(orderItem);
            order.setActualAmount(productSkuMapper.selectById(Integer.parseInt(orderItem.getProductId())).getSellPrice());
        }

        System.out.println("________________");
        System.out.println(order);
        orderMapper.insert(order);
        return s1;
    }

    public Order orderAdd(GoodsMessage goodsMessage,String s){
        Order order = new Order();
        order.setOrderId(s);//存入订单号
        order.setUserId(goodsMessage.getUserId());//所属用户Id

        UserAddr userAddr = userAddrMapper.selectById(goodsMessage.getUserId());
        order.setReceiverName(userAddr.getReceiverName());
        order.setReceiverMobile(userAddr.getReceiverMobile());
        //地址
        order.setReceiverAddress(userAddr.getProvince()+"  "+userAddr.getCity()+"  "+userAddr.getArea()+"  "+userAddr.getAddr());
        order.setCreateTime(new Date());
        return order;
    }

    public OrderItem orderItemAdd(GoodsMessage goodsMessage,String s,Integer i){
        OrderItem orderItem = new OrderItem();
        orderItem.setOrderId(s);
        String s1 = null;
        if (i<10){//生成的订单按照，分割
            s1 = orderNumber.orderNumber(i);
            orderItem.setItemId(s1);
        }
        else{
            int j=i%10;
            s1 = orderNumber.orderNumber(j);
            orderItem.setItemId(s1);
        }
        String[] split = goodsMessage.getShopId().split(",");
        int i1 = Integer.parseInt(split[i]);
        System.out.println(i);
        ShoppingCart shoppingCart = iShoppingCartAllInfoService.selectOne(i1);

        orderItem.setProductId(shoppingCart.getProductId().toString());//商品id
        orderItem.setProductName(productIdMapper.selectById(Integer.parseInt(orderItem.getProductId())).getProductName());
//        orderItem.setProductImg(productImgMapper.selectById(orderItem.getProductId()).getUrl());
        orderItem.setProductPrice(productSkuMapper.selectById(orderItem.getProductId()).getSellPrice());
        orderItem.setBuyCounts(shoppingCart.getCartNum());
        //总价
        orderItem.setTotalAmount(orderItem.getProductPrice() * orderItem.getBuyCounts());

        return orderItem;
    }

    @Override
    public Integer queryId(String name) {
        return usersMapper.queryByName(name);
    }

    @Override
    public void delOrderId(String orderId) {
        orderMapper.deleteById(orderId);
    }


}
